<?php
namespace App\Repositories\Presenter;
class CategoryPresenter{

    public function getMenuList($menus){
        if($menus){
            $item = '';
            foreach( $menus as $v ){
                $item .= self::getNestablItem($v['id'],$v['name'],$v['child'],$v['sort'],$v['alias'],$v['tips'],$v['isShow'],$v['created_at']);
            }
            return $item;
        }
        return '暂无菜单';
    }

    /**
     * 获取菜单数据
     * @param $id
     * @param $name
     * @param $child
     * @param $sort
     * @param $alias
     * @param $tips
     * @param $isShow
     * @param $creatime
     * @return string
     */
    protected function getNestablItem($id,$name,$child,$sort,$alias,$tips,$isShow,$creatime){
        if( $child ){
            return $this -> getHandList($id,$name,$child,$sort,$alias,$tips,$isShow,$creatime);
        }
        return '<tr><td class="tc"><input type="text" name="ord[]" value="'.$sort.'"></td><td class="tc">'.$id.'</td><td>'.$name.'</td><td>'.$alias.'</td><td>'.$tips.'</td><td>'.$this->isShow($isShow).'</td><td>'.$creatime.'</td><td><a href="'.url(' admin/category/'.$id.'/edit').'">修改</a><a href="javascript:;" onclick="delCat('.$id.')">删除</a></td></tr>';
    }

    /**
     * 获取子菜单数据
     * @param $id
     * @param $name
     * @param $child
     * @param $sort
     * @param $alias
     * @param $tips
     * @param $isShow
     * @param $creatime
     * @return string
     */
    protected function getHandList($id,$name,$child,$sort,$alias,$tips,$isShow,$creatime){
        $hanld = '';
        $hanld .= '<tr><td class="tc"><input type="text" name="ord[]" value="'.$sort.'"></td><td class="tc">'.$id.'</td><td>'.$name .'</td><td>'.$alias.'</td><td>'.$tips.'</td><td>'.$this->isShow($isShow).'</td><td>'.$creatime.'</td><td><a href="'.url(' admin/category/'.$id.'/edit').'">修改</a><a href="javascript:;"  onclick="delCat('.$id.')">删除</a></td></tr>';
        foreach($child as $v){
            $hanld .= $this->getNestablItem($v['id'],'├─'.$v['name'],$v['child'],$v['sort'],$v['alias'],$v['tips'],$v['isShow'],$v['created_at']);
        }

        return $hanld;
    }
    protected function isShow($show){
        if( $show === 1 ){
            return '是';
        }else{
            return '否';
        }
    }
}